package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.pojo.cart.Cart;
import com.dongdongshop.pojo.order.TbOrder;
import com.dongdongshop.pojo.order.TbOrderItem;
import com.dongdongshop.pojo.user.TbAddress;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.cart.CartService;
import com.dongdongshop.service.order.OrderService;
import com.dongdongshop.service.user.AddressService;
import com.dongdongshop.util.IdWorker;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author Eayon
 * @Date 2020/3/14 20:28
 */
@Service
@Component
public class OrderServiceImpl implements OrderService {

    @Resource
    private TbOrderMapper orderMapper;

    @Resource
    private IdWorker idWorker;

    @Reference(check = false)
    private AddressService addressService;

    @Resource
    private TbOrderItemMapper orderItemMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Reference(check = false)
    private CartService cartService;

    /**
     * 创建订单
     * @param cartList
     * @param id
     * @param user
     */
    @Override
    public void addOrder(List<Cart> cartList, Long id, TbUser user) {
        //查询该收件人地址
        TbAddress address = addressService.getAddressById(id);
        //从该用户的redis购物车中删除该商品明细  用于删除的
        List<Cart> carts = (List<Cart>)redisTemplate.boundHashOps("cartList").get(user.getUsername());

        for (Cart cart : cartList) {
            TbOrder order = new TbOrder();
            Long orderId = idWorker.nextId();//雪花算法生成订单Id
            order.setOrderId(orderId);//订单号
            order.setStatus("1");//订单状态 未付款
            order.setCreateTime(new Date());//订单创建时间
            order.setUserId(user.getId().toString());//买家id
            order.setBuyerMessage(user.getName());//买家昵称
            order.setReceiverAreaName(address.getAddress());//收货地址
            order.setReceiverMobile(address.getMobile());//收货人手机
            order.setReceiver(address.getContact());//收货人姓名
            order.setSellerId(cart.getSellerId());//商家id
            orderMapper.insertSelective(order);

            //商品明细
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            for (TbOrderItem orderItem : orderItemList) {
                TbOrderItem ot = new TbOrderItem();
                ot.setId(idWorker.nextId());//主键id
                ot.setItemId(orderItem.getItemId());//该商品明细订单关联的商品明细的主键id
                ot.setGoodsId(orderItem.getGoodsId());//spuId
                ot.setOrderId(orderId);//订单id
                ot.setTitle(orderItem.getTitle());//标题
                ot.setPrice(orderItem.getPrice());//单价
                ot.setNum(orderItem.getNum());//数量
                ot.setTotalFee(orderItem.getTotalFee());//总价钱
                ot.setPicPath(orderItem.getPicPath());//图片
                ot.setSellerId(cart.getSellerId());//商家id
                //添加到商品明细表
                orderItemMapper.insertSelective(ot);
                //从该用户的redis购物车中删除该商品明细
                carts = cartService.deleteCartOrderItem(carts, orderItem.getItemId());
            }
        }
        //最后将删除后的购物车重新添加到该用户的redis中
        cartService.saveCartListToRedis(carts,user.getUsername());
    }
}
